<?php
	$text=$_POST['text'];  //文本信息
	$encrypt=$_POST['encrypt'];  //加密按钮
	$decrypt=$_POST['decrypt'];  //解密按钮
	
	//密钥图			
	$keys = array(array(' ','A','B','C','D','E'),
							array('A','a','b','c','d','e'),
							array('B','f','g','h','i','k'),
							array('C','l','m','n','o','p'),
							array('D','q','r','s','t','u'),
							array('E','v','w','x','y','z'));
	
	//加密函数
	function encryption( $plaintext, $keys ) {
		$ciphertext = ""; //密文字符串
		$textlen = strlen($plaintext);  //计算字符串长度
		$char = strtok($plaintext,'');  //把字符串转换为数组	
		// 循环加密
		for($num = 0; $num < $textlen; $num++ ){
			for($row = 1; $row < 6; $row++ ) //行
				for($col = 1; $col < 6; $col++) {  //列
					if(strcmp($keys[$row][$col], $char[$num]) == 0 ) {
						$ciphertext = $ciphertext.$keys[$row][0].$keys[0][$col];
						break;
					}
				}
		}
		return $ciphertext;			 
	}

	//解密函数	
	function decryption( $ciphertext, $keys ) {
		$plaintext = ""; //明文字符串
		$textlen = strlen($ciphertext);  //计算字符串长度
		$char = strtok($ciphertext,'');	 //得到字符数组	
		for($num = 0; $num < $textlen; $num++ ) {
			for($row = 1; $row < 6; $row++ ) //行
				if( strcmp($keys[$row][0], $char[$num]) == 0) {
					$num++;
					for($col = 1; $col < 6; $col++) //列
						if(strcmp($keys[0][$col], $char[$num]) == 0 ) 
							$plaintext = $plaintext.$keys[$row][$col];
						break;
				}
		}
		return $plaintext;
	}
	
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PHP版Polybios算法演示</title>
</head>
<body>
	<h2 align="center" style="font-size:25px; "> PHP版<i>Polybios</i>算法演示 </h2>
	<br />

	<table align="center" border="0" >
	<tbody><tr><td>	
	<form action="polybios.php" method="post" >
		<div><label for="text">请在文本框输入要处理的信息</label>
		</div>
		<?php
			if(isset($text)) {
				echo '<div><textarea name="text" >'.$text.'</textarea></div>';
			}
			else
				echo '<div><textarea name="text" ></textarea></div>';
		?>
		<div>	<input type="submit" name="encrypt" value="加密" />
			<input type="submit" name="decrypt" value="解密" />
			<input type="reset" name="reset" value="重置" />
		</div>
		<div></div>
	</form>
	</td><tr></tbody>

	<tbody><tr><td>
	<?php
		if( !empty($text) ) {
			echo '<form ><div><label for="result">经处运算理后结果为</label></div>';
			echo '<div><textarea name="result" readonly >';
	?>	
	<?php		
			if( isset($encrypt) )
				echo encryption( $text, $keys);
			elseif( isset($decrypt) )
				echo decryption( $text, $keys );
			else 
				echo 'Error !';
	?>
	<?php
			echo '</textarea></div></form>';
		}
	?>
	</td><tr></tbody>
	</table>
	
	<p style="text-indent: 4mm" >Polybios密码体制： 加密算法的规则是明文中的每个字母都用字母所在的行和列的字母代替。主要缺点：有数据膨胀问题，不安全。
	</p>
	<p align="center" >本次加密算法使用的密钥图:
	</p>
	<table align="center" border="1" >
		<?php
			for($row = 0; $row < 6; $row++ ) {
				echo '<tr>';
					for($col = 0; $col < 6; $col++ )
						echo '<td>&nbsp;'.$keys[$row][$col].'</td>';
				echo '</tr>';			
			}
		?>
	</table>	
	
<!--footer-->
	<br /><br /><table align="center">
		<tr><td>
			<p><font style="font-size:10px; background:lightskyblue;"> Designed by <a href="http://mifunny.info/" title="LD's Blog">LD</a>.  A better web experience in Firefox.</font></p>
		</td></tr>
	</table>
</body>
</html>